// index.js
// 获取应用实例
const app = getApp()
import utils from '../../utils/util.js'

Page({
  data: {
    showAuth: false,
    noticeList: null,
    noMore: false,
    indexData: {
      schCount: '12',
      taskCount: '3',
      fieldCount: '4',
      cropCount: '1'
    }
  },

  onLoad() {
    //this.userLogin()
    // let isLogin = utils.checkLogin()
    // if(!isLogin){
    //   //弹出登录授权框
    //   this.setData({showAuth: true})
    // }else{
    // }
    this.loadNotices()
  },
  
  onCloseAuth: function(){
    this.setData({showAuth: false})
    this.loadNotices()
  },

  //查询倾诉信息
  loadNotices: function(){
    let that = this
    let req = {}
    if(this.data.noticeList!=null){
      req.total=this.data.noticeList.totalrecord,
      req.page=this.data.noticeList.currentpage
    }
    utils.request(
      app.globalData.apiBaseUrl+'/notice/list',
      req,
      'POST'
    ).then(res=>{
      wx.stopPullDownRefresh()
      let existList = that.data.noticeList
        if(existList!=null){
          existList.records=existList.records.concat(res.data.records)
          that.setData({
            noticeList: existList
          })
        }else{
          that.setData({
            noticeList: res.data,
            totalrecords: res.data.totalrecords,
            page: res.data.page
          })
        }
        if(res.data.currentpage==res.data.totalpage){
          that.setData({noMore: true})
        }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({noticeList: null})
    this.loadNotices()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.noticeList==null || this.data.noticeList.currentpage==this.data.noticeList.totalpage){
      this.setData({noMore:true})
      return
    }
    let tmp = this.data.noticeList
    tmp.currentpage=tmp.currentpage+1
    this.setData({noticeList: tmp})
    this.loadNotices()
  },

  //浏览大图
  showLargeImage(e) {
    let currIdx = e.currentTarget.dataset.idx //被点击的图片在该笔记中的下标，比如有五张图片，被点的是第3张，currIdx则为2
    let logidx = e.currentTarget.dataset.logidx //被点的图片所在的笔记在noticeList中的下标
    let curr = ''
    let imgList = []
    for(var idx=0; idx<this.data.noticeList.records[logidx].picList.length; idx++){
      imgList.push(this.data.noticeList.records[logidx].picList[idx])
      if(currIdx==idx){
          curr = this.data.noticeList.records[logidx].picList[idx]
      }
    }
    wx.previewImage({
        urls: imgList, //需要预览的图片http链接列表，注意是数组
        current: curr, // 当前显示图片的http链接，默认是第一个
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
  },

  toAddPage: function(){
    wx.navigateTo({
      url: '/pages/notice/publish',
    })
  },

  //进入详情
  toBlogDetail: function(e){
    let bid = e.currentTarget.dataset.noticeid
    console.log('跳转到详情页',bid)
    wx.navigateTo({
      url: '/pages/notice/notice-detail?nid='+bid,
    })
  },

  //点赞
  onLike: function(e){
    let bid = e.currentTarget.dataset.noticeid
    let idx = e.currentTarget.dataset.listindex
    let haslike = e.currentTarget.dataset.haslike
    let that = this
    let req = {bid: bid}
    utils.request(
      app.globalData.apiBaseUrl+'/notice/like',
      req
    ).then(res=>{
      let notices = that.data.noticeList
      if(haslike==1){
        notices.records[idx].hasLike=0
        notices.records[idx].likes=notices.records[idx].likes-1
      }else{
        notices.records[idx].hasLike=1
        notices.records[idx].likes=notices.records[idx].likes+1
      }
      that.setData({noticeList: notices})
    })
  },
})
